﻿using System;
using System.Collections.Generic;
using System.Linq;
using Fuse8.ContentManagementFramework.Domain.Commands.CmsUser;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsUserGetByUserGroupDataCommand : CmsUserGetByUserGroupCommand
    {
        public override IQueryable<Domain.CmsUser> Execute()
        {
            IEnumerable<Domain.CmsUser> result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<Domain.CmsUser>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<Domain.CmsUser>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<Domain.CmsUser, int>>();

                result = SqlUtility<Domain.CmsUser, int>.ExecuteReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                         "cms_sp_UsersSelectForUserGroup",
                        new
                        {
                            SiteId = CommandParameter.SiteId,
                            SiteGroupId = CommandParameter.SiteGroupId,
                            UserGroupId = CommandParameter.UserGroupId
                        },
                        mapper);
            }

            return result != null ?
                result.AsQueryable() :
                new List<Domain.CmsUser>().AsQueryable();
        }
    }
}
